import { FormItemRule } from "element-plus";
import { FormItemOptions } from "./type";

export class FormItem<T> {
	private options: FormItemOptions<T>;
	constructor(options: FormItemOptions<T>) {
		this.options = options;
	}

	/**设置是否禁用 */
	setDisabled(disabled: boolean) {
		this.options.disabled = disabled;
	}

	/**
	 * 设置表单项配置
	 * @param options
	 */
	setOptions(options: FormItemOptions<T>) {
		this.options = options;
	}

	/**
	 * 获取表单项配置
	 * @returns
	 */
	getOptions() {
		return this.options;
	}

	/**
	 * 设置表单的值
	 */
	setValue(value: T[keyof T]) {
		this.options.value = value;
	}

	/**
	 * 获取表单的值
	 */
	getValue() {
		if (this.options.component === "InputNumber") {
			if (typeof this.options.value === "string") {
				return (Number(this.options.value.trim()) || 0) as T[keyof T];
			}
		}
		return this.options.value;
	}

	/**设置校验规则 */
	setRule(rule: Array<FormItemRule>) {
		this.options.rule = rule;
	}
}
